Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmMainForm
	Inherits System.Windows.Forms.Form 'Declares that all variables must be defined before use.
	
	'**********************************************************************************************************************************************************************
	'Helium Frog stop motion software - by Martin Price - see www.HeliumFrog.com or www.KeysoeAnimation.co.uk
	
	'Note :- This program requires CapStill.dll and FSFWrap.dll to be copied to the C:/WINDOWS/System32/ directory and then registered.
	'To register the .dll files use windows start menu then click run. type regsvr32 capstill.dll and then hit enter. Then type regsvr32 fsfwrap.dll then enter
	'You must also distribute these .dlls when sending the program out using molebox etc.
	'**********************************************************************************************************************************************************************
	
	Dim MonthNum As String 'The month of the year. Used to set the initial project name
	Dim DayNum As String 'The day of the month. Used to set the initial project name
	Dim FileNumber As String 'The string of the file number "001" "002" etc.
	Dim intFileNumber As Short 'The integer of the file number
	Public Sub PrintMainCaption() 'Prints the main program caption
		If SoundTrackOnOff = True Then
			Me.Text = "The Helium Frog Animator V" & Version & " - By Martin Price"
		Else
			Me.Text = "The Helium Frog Animator V" & Version & " - By Martin Price     Sound Off"
		End If
	End Sub
	
	Private Sub frmMainForm_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		
		On Error Resume Next
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "/Errorlog.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "/Errorlog.txt")) 'Erase the error log file if it exists
		
		On Error GoTo ErrorHandling 'If an error occurs go to the error handling routine
		
		ErrorCodeNo = "00.99" 'Set an error code for the next section of program
		DiagnosticsYesNo = False
		
		Call SetDefaults() 'Calls the default variable setting in Declares module
		
		intFileNumber = 1 'Set the initial filenumber to 1
		
		
		
		Me.Left = 0 'Set the Main form to be at the top of the screen
		Me.Top = 0 'Set the Main form to be at the left of the screen
		Me.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width 'Set the Main form to be as wide as possible on the screen
		Me.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height 'Set the Main form to be as tall as possible on the screen
		DisableCloseWindowButton(Me) 'Disables the close button at the top right of the main form
		Me.Show() 'Make the main form visible to the user
		'These two lines seem to ensure that a messy setup of the capture window is not visible to the user.
		
		
		frmCapture.WindowState = System.Windows.Forms.FormWindowState.Normal 'Ensure the capture window is not minimised (May not be necessary)
		
		
		ErrorCodeNo = "00.01" 'Set an error code for the next section of program
		'If a user settings file exists load all the Program settings (Window sizes etc.) from the User_settings.txt file
		'This overwrites the variables initially setup in the declares module.
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\Settings_User.txt") <> "" Then Call LoadSettingData()
		
		RotoScopeLoadedYesNO = False 'Tell the program that no film has been loaded in rotoscope (so capture doesnt try to advance it)
		RotoScopeActiveYesNo = False 'Tell the program that the rotoscope form is not showing
		PlayStatus = "Stopped"
		
		ErrorCodeNo = "00.02"
		
		Call PrintMainCaption() 'Print the main program caption
		
		ErrorCodeNo = "00.03"
		Call AudioPlayer_Create(SoundTrackName) 'Load the specified soundtrack into the audio player
		
		
		'Check that all the necessary files exist to run the program
		Call CheckFileExists("\Skins\" & Skin & "_Buttons_H.bmp")
		Call CheckFileExists("\Skins\" & Skin & "_Buttons_S.jpg")
		Call CheckFileExists("\Skins\" & Skin & "_Settings_H.bmp")
		Call CheckFileExists("\Skins\" & Skin & "_Settings_S.jpg")
		Call CheckFileExists("\Skins\" & Skin & "_Source_H.bmp")
		Call CheckFileExists("\Skins\" & Skin & "_Source_S.jpg")
		Call CheckFileExists("\Skins\" & Skin & "_Editor_H.bmp")
		Call CheckFileExists("\Skins\" & Skin & "_Editor_S.jpg")
		Call CheckFileExists("\Skins\Front_Screen.jpg")
		Call CheckFileExists("\Sounds\Camera_Click.wav")
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\Sounds\" & CaptureSound & ".wav") = "" Then 'I cant find the sound, so resort to the default sound
			CaptureSound = "Camera_Click"
		End If
		
		
		
		
		ErrorCodeNo = "00.04"
		'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
        'Load(frmLoading) 'Load the loading form splash screen this runs the form load routine to set it up but doesnt show it yet
		frmLoading.Show() 'The form is setup, so show it to the user
		
		
		frmLoading.lblLoadInd.Text = "  >>" 'Move the progress bar along
		frmLoading.Refresh()
		
		'Set up the memory DCs that hold the "hotspot" images in memory for each of the skins of each window
		Hotspot_Source = LoadGraphicDC(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Source_H.bmp")
		Hotspot_Buttons = LoadGraphicDC(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Buttons_H.bmp")
		Hotspot_Settings = LoadGraphicDC(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Settings_H.bmp")
		Hotspot_Editor = LoadGraphicDC(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Editor_H.bmp")
		
		frmLoading.lblLoadInd.Text = "  >>>>" 'Move the progress bar along
		frmLoading.Refresh()
		
		'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
        'Load(frmAveraging) 'Load the form that holds all the pictureboxes. This is invisible to the user
        'TODO - Need to look at this - Can;t call Load event in VB.Net

		'Define the settings for the live picture box which initially recieves the camera feed (must be visible to work but make very small)
		'This doesnt show on the main form for some reason (no bad thing!), but still works. Is it very small because it is in twips? not sure yet!
		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
		'UPGRADE_ISSUE: PictureBox property picCamera.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picCamera.ScaleMode = vbPixels : frmAveraging.picCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize : frmAveraging.picCamera.Left = 0 : frmAveraging.picCamera.Top = 0
		'UPGRADE_ISSUE: PictureBox property picCamera.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picCamera.AutoRedraw = True : frmAveraging.picCamera.BorderStyle = System.Windows.Forms.BorderStyle.None
		frmAveraging.picCamera.Visible = True
		
		'use the picCamera picture as a temporary store to only pre calculate the empty areas of the skins
		frmAveraging.picCamera.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Source_H.bmp") 'Setup the correct picture size etc.
		RegionSource = MakeRegion((frmAveraging.picCamera)) 'Call the subroutine to calculate the non transparent area (active area) of the picturebox
		
		frmLoading.lblLoadInd.Text = "  >>>>>>" 'Change the loading progress bar
		frmLoading.Refresh()
		
		frmAveraging.picCamera.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Buttons_H.bmp") 'Setup the correct picture size etc.
		RegionButtons = MakeRegion((frmAveraging.picCamera)) 'Call the subroutine to calculate the non transparent area (active area) of the picturebox
		
		frmLoading.lblLoadInd.Text = "  >>>>>>>>" 'Change the loading progress bar
		frmLoading.Refresh()
		
		frmAveraging.picCamera.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Settings_H.bmp") 'Setup the correct picture size etc.
		RegionSettings = MakeRegion((frmAveraging.picCamera)) 'Call the subroutine to calculate the non transparent area (active area) of the picturebox
		
		frmLoading.lblLoadInd.Text = "  >>>>>>>>>>" 'Change the loading progress bar
		frmLoading.Refresh()
		
		frmAveraging.picCamera.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Settings_H.bmp") 'Setup the correct picture size etc.
		RegionUtilities = MakeRegion((frmAveraging.picCamera)) 'Call the subroutine to calculate the non transparent area (active area) of the picturebox
		
		frmLoading.lblLoadInd.Text = "  >>>>>>>>>>>>" 'Change the loading progress bar
		frmLoading.Refresh()
		
		frmAveraging.picCamera.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Editor_H.bmp") 'Setup the correct picture size etc.
		RegionEditor = MakeRegion((frmAveraging.picCamera)) 'Call the subroutine to calculate the non transparent area (active area) of the picturebox
		
		If DiagnosticsYesNo = True Then 'if diagnostics are required, load and show the form
			'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
            'Load(frmDiagnostics)
			Call frmDiagnostics.ShowDiagnostics() 'call the routine to obtain all the values
			frmDiagnostics.Show()
		End If
		
		ErrorCodeNo = "00.05"
		'UPGRADE_ISSUE: PictureBox property picCamera.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'

        Dim avgGraph As Graphics = frmAveraging.picCamera.CreateGraphics
        Dim avgHDC As IntPtr = avgGraph.GetHdc

        Result = CreateCompatibleDC(avgHDC) 'Create a memory device context (DC) area compatible with the camera destination picturebox

        avgGraph.ReleaseHdc(avgHDC)
        avgGraph.Dispose()
		
		ErrorCodeNo = "00.06"
		'Generate an avi filename.
		MonthNum = CStr(Month(Today)) 'Get the month of the year
		DayNum = CStr(VB.Day(Today))
		If Len(MonthNum) = 1 Then MonthNum = "0" & MonthNum 'If its a single digit month, add a leading zero
		If Len(DayNum) = 1 Then DayNum = "0" & DayNum 'If its a single digit day, add a leading zero
CheckNameAgain: 
		FileNumber = LTrim(Str(intFileNumber))
		If Len(FileNumber) = 1 Then FileNumber = "00" & FileNumber
		If Len(FileNumber) = 2 Then FileNumber = "0" & FileNumber
		ProjName = Year(Today) & "_" & MonthNum & "_" & DayNum & "_Animation_" & FileNumber 'Set the default filename
		' Check if the file exists
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SavePath & ProjName & ".avi") <> "" Then
			intFileNumber = intFileNumber + 1 'The file exists so increment the number
			GoTo CheckNameAgain
		End If
		
		ErrorCodeNo = "00.07"
		Call LoadSound() 'Load the sounds into memory
		
		ErrorCodeNo = "00.08"
		frmLoading.Close() 'Unload the startup splash screen form. This hides it and unloads all the variables etc.
		
		ErrorCodeNo = "00.09"
		'UPGRADE_ISSUE: Load statement is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321"'
        'Load(frmSourceSetup) 'Load the source setup screen. Control is transfered to the form load routine of that form
        'TODO - Can;t Call Form Load in VB.Net - Look into how to handle this		
		ErrorCodeNo = "00.99"
		On Error GoTo 0 'Set error handling so it doesnt return here.
		
		Exit Sub 'Exit the subroutine
		
		'***********************************************************************************************************************************************
ErrorHandling: 
		ErrorDescription = "Capture Routine Problem"
		Call frmErrorBox.ErrorMessageBox() 'Put up a message Box
		Call ExitProgram() 'Exit the Program gracefully
	End Sub
	
	Private Sub frmMainForm_FormClosing(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		Dim Cancel As Boolean = eventArgs.Cancel
		Dim UnloadMode As System.Windows.Forms.CloseReason = eventArgs.CloseReason 'This is called if the Form 'X' button has been pressed
		ErrorCodeNo = "00.00" 'Tell the program it is a normal exit
		Call ExitProgram()
		eventArgs.Cancel = Cancel
	End Sub
	
	Private Sub CheckFileExists(ByRef FileNm As String)
		'This subroutine checks that a file exists and reports if it doesn't then exits the program
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & FileNm) = "" Then
			Beep()
			Call MsgBox("I cannot find file " & FileNm & " so I cannot continue", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			End
		End If
		
	End Sub
End Class